package com.migo.api;

import com.migo.annotation.IgnoreAuth;
import com.migo.entity.FlightLogEntity;
import com.migo.service.FlightLogService;
import com.migo.utils.DateUtils;
import com.migo.utils.R;
import com.migo.validator.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Wes on 2018/12/25.
 */
@RestController
@RequestMapping("/api")
public class ApiFlyLogController {


    @Autowired
    private FlightLogService flightLogService;

    /**
     * 查询飞行日志
     */
    @IgnoreAuth
    @RequestMapping(value = "queryFlyLog", produces = "application/json;charset=utf-8;")
    public R queryFlyLog(@RequestParam Map<String, String> params) {
        String userId = params.get("userId");
        String productId = params.get("productId");
        String time = params.get("time");
        try {
            Assert.isBlank(userId, "userId is null");
            Assert.isBlank(productId, "productId is null");
            Assert.isBlank(time, "time is null");
            //查询列表数据
            Map<String,Object> param = new HashMap<>();
            param.put("userId",userId);
            param.put("productId",productId);
            if(time.equals("1")||time.equals("2"))
            {
                int days = time.equals("1")?30:30*6;
                Date date = DateUtils.nextDay(new Date(),days);
                String beginTime =  DateUtils.date2String(new Date(),"yyyy-MM-dd HH:MM:ss");
                String endTime  = DateUtils.date2String(date,"yyyy-MM-dd HH:MM:ss");
                param.put("beginTime",beginTime);
                param.put("endTime",endTime);
            }
            List<FlightLogEntity> flightLogList = flightLogService.queryList(param);
            return R.ok().put("data", flightLogList);
        } catch (Exception e) {
            return R.error("send verification code error.");
        }

    }
}
